﻿using GodSystemBLL;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormUserAdd : Form
    {
        public FormUserAdd()
        {
            InitializeComponent();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            string name = textBox_Name.Text.Trim();
            string password = textBox_Password.Text.Trim();
            string rePassword = textBox_Repassword.Text.Trim();
            bool sexMan = radioButton_man.Checked;
            bool sexWoman = radioButton_woman.Checked;
            string department = textBox_Department.Text.Trim();
            if(string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(rePassword))
            {
                MessageBox.Show("用户名和密码不能为空","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!password.Equals(rePassword))
            {
                MessageBox.Show("两次密码不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if(name.Length < 5 || name.Length > 12)
            {
                MessageBox.Show("用户名长度为5-12位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (password.Length < 5 || password.Length > 16)
            {
                MessageBox.Show("密码长度为5-16位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!sexMan && !sexWoman)
            {
                MessageBox.Show("性别必须选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            UsersBLL usersBLL = new UsersBLL();
            int userTotal = Convert.ToInt32(usersBLL.CheckUserNameCount(name));
            if(userTotal > 0)
            {
                MessageBox.Show("用户名【 "+name+" 】已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            UsersModel user = new UsersModel();
            user.Name = name;
            user.Password = password;
            user.Sex = sexMan ? 1 : 0;
            user.Department = department;
            user.IsSuperManager = 0;
            int affactNum = usersBLL.AddUser(user);
            if(affactNum > 0)
            {
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox_Name.Text = "";
                textBox_Password.Text = "";
                textBox_Repassword.Text = "";
                textBox_Department.Text = "";
                return;
            }
            else
            {
                MessageBox.Show("添加失败，请稍后再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}
